<%@ page language="java" contentType="text/html; charset=utf-8" %>
<jsp:useBean id="rs" class="weaver.conn.RecordSet" scope="page"/>
<jsp:useBean id="bb" class="weaver.general.BaseBean"/>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="org.apache.axis.encoding.Base64" %>
<%@ page import="weaver.docs.webservices.DocAttachment" %>
<%@ page import="weaver.docs.webservices.DocInfo" %>
<%@ page import="weaver.docs.webservices.DocServiceImpl" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.hrm.User" %>
<%@ page import="weaver.tools.util.CanvasToPDF" %>
<%@ page import="weaver.workflow.request.RequestComInfo" %>
<%@ page import="java.io.ByteArrayOutputStream" %>
<%@ page import="java.io.File" %>
<%@ page import="java.io.FileInputStream" %>
<%@ page import="java.io.InputStream" %>
<%
    JSONObject jsonData = new JSONObject();
    String requestid = Util.null2String(request.getParameter("requestid"));
    String data = Util.null2String(request.getParameter("imgdata"));
    boolean flag = false;
    try {
        RequestComInfo comInfo = new RequestComInfo();
        String requestname = comInfo.getRequestname(requestid);
        String path = Util.null2String(bb.getPropValue("pdf", "pdfpath"));
        String pdfname = path + requestname + ".pdf";
        bb.writeLog("pdfname>>>>>" + pdfname);
        CanvasToPDF canvasToPDF = new CanvasToPDF();
        canvasToPDF.toPdf(data, pdfname);
        File pdfFile = new File(pdfname);
        if (pdfFile.exists() && pdfFile.length()>0) {
            //上传附件
            int docid = createDoc(requestname, pdfname, 5);
            if (docid > 0) {
                flag = rs.executeUpdate("update formtable_main_17 set fjsc=? where requestid=?", docid, requestid);
            }

        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    jsonData.put("flag", flag);
    out.print(jsonData);
%>
<%!
    //创建文档
    public static int createDoc(String rawFileName, String filepath, int secid) {

        BaseBean baseBean = new BaseBean();
        baseBean.writeLog(">>>>>>createDocs>>>>>>");
        byte[] content = new byte[102400];
        // 上传附件，创建html文档
        content = null;
        try {
            int byteread;
            byte data[] = new byte[1024];
            InputStream input = new FileInputStream(new File(filepath));
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            while ((byteread = input.read(data)) != -1) {
                out.write(data, 0, byteread);
                out.flush();
            }
            content = out.toByteArray();
            input.close();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        int docid = -1;
        DocServiceImpl docserviceimpl = new DocServiceImpl();
        DocInfo doc = new DocInfo(); //创建文档
        String keyword = ""; //文档内容
        try {
            DocAttachment[] docattachment = new DocAttachment[1];
            baseBean.writeLog(">>>>>rawFileName=" + rawFileName);
            String filetype = "";
            if (rawFileName.lastIndexOf(".") > -1) {
                filetype = rawFileName.substring(rawFileName.lastIndexOf(".") + 1);
            }
            // 上传附件，创建html文档
            DocAttachment da = new DocAttachment();
            da.setFilename(rawFileName);//附件标题
            da.setFilecontent(Base64.encode(content)); //附件内容
            da.setFiletype(filetype); //imagefiletype
            da.setImagefilename(rawFileName);// 附件标题
            da.setIszip(1);//是否压缩
            da.setIsAesEncrype(0);//是否加密
            da.setAesCode("0");
            da.setIsextfile("1"); //附件是否是office类型

            if (filetype.equalsIgnoreCase("doc")) {
                da.setDocfiletype("3");
            } else if (filetype.equalsIgnoreCase("xls")) {
                da.setDocfiletype("4");
            } else if (filetype.equalsIgnoreCase("ppt")) {
                da.setDocfiletype("5");
            } else if (filetype.equalsIgnoreCase("wps")) {
                da.setDocfiletype("6");
            } else if (filetype.equalsIgnoreCase("docx")) {
                da.setDocfiletype("7");
            } else if (filetype.equalsIgnoreCase("xlsx")) {
                da.setDocfiletype("8");
            } else if (filetype.equalsIgnoreCase("pptx")) {
                da.setDocfiletype("9");
            } else {
                da.setDocfiletype("2");
            }
            docattachment[0] = da;
            keyword = rawFileName;
            doc.setAttachments(docattachment);
            doc.setSeccategory(secid);//子目录id
            doc.setDocSubject(keyword);//标题
            doc.setDoccontent("");//文档内容
            doc.setKeyword(keyword);
            docid = docserviceimpl.createDocByUser(doc, new User(1));
            baseBean.writeLog(">>>>>docid=" + docid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return docid;
    }

%>